<%-- 
    Document   : search
    Created on : Mar 12, 2012, 3:22:46 PM
    Author     : Alberto
--%>

<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="edu.harding.comp431.Speet"%>
<%@page import="edu.harding.comp431.SpitterUser"%>
<%@page import="edu.harding.comp431.Database"%>
<%@page import="edu.harding.comp431.ImageUtilities"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="user"
             class="edu.harding.comp431.SpitterUser"
             scope="session" />

<%    
    // Make sure the user is logged in
    if (session.getAttribute("login") == null) {
        response.sendRedirect("login.jsp");
        return;
    }
    
    
    // Get database from the application context
    Database spitterDatabase = (Database)application.getAttribute("spitterDatabase");
    if (spitterDatabase == null) {
        response.sendRedirect("ActionServlet?action=view");
        return;
    }
    
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spitter</title>
        
        <link rel="stylesheet" type="text/css" href="styles.css" />
        
        <script type="text/javascript" src="http://cs.harding.edu/fmccown/jquery.js"></script>
        <script type="text/javascript" src="main.js"></script>
    </head>
    <body>
        <%@ include file="heading.jspf" %>
        
        <div class="section profile">
            <span class="main-heading">People</span>
            <br /><br />
            
            <%
                String search = request.getParameter("query");
                ArrayList<SpitterUser> list = spitterDatabase.searchForUser(search, user.getUsername());

                if (list.size() == 0) 
                {
                    %>
                    No match results.
                    <%
                }
                else 
                {
                    for (SpitterUser u : list) 
                    {                             
                        String imageFile = u.getUsername() + ".jpg"; 
                        String fullPath = this.getServletContext().getRealPath(
                                    "images\\" + imageFile);
                        if (!ImageUtilities.imageExists(fullPath))
                            imageFile = "default.jpg";                                                               
            %>
            <table
            <tr>
                <td valign="top">
                    <img src="images/<%= imageFile %>" class="followers-profile-image"/>
                    <br />
                    <% if (u.isFollowing()) 
                        { %>    
                            &nbsp;<a class="unfollow-link" 
                            href="ActionServlet?action=unfollow&user=<%= u.getUsername() %>&username=<%= user.getUsername() %>">Unfollow</a>
                    <% } else 
                        { %>
                            &nbsp;  <a class="follow-link" 
                            href="ActionServlet?action=follow&user=<%= u.getUsername() %>&username=<%= user.getUsername() %>">Follow</a>
                    <% } %>  
                </td>
                <td width="100%">
                    <span class="profile-username"><%= u.getUsername() %></span>
                    <br />
                    <span class="profile-about"><%= u.getAbout() %></span>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <hr />
                </td>
            </tr>
        </table>
        
        <% } } %>
         </div>   
        <div class="section speets">
             <span class="main-heading">Search results for <%= search %></span>
            <br /><br />
            
            <%
                ArrayList<Speet> speets = spitterDatabase.searchForSpeets(search);

                if (speets.size() == 0) 
                {
                    %>
                    No match results.
                    <%
                }
                else 
                {
                    for (Speet s : speets) 
                    {                             
                        String imageFile = s.getUsername() + ".jpg"; 
                        String fullPath = this.getServletContext().getRealPath(
                                    "images\\" + imageFile);
                        if (!ImageUtilities.imageExists(fullPath))
                            imageFile = "default.jpg";   
            %>
           <table>
            <tr>
                <td valign="top">
                    <img src="images/<%= imageFile %>" class="speet-profile-image"/>

                </td>
                <td width="100%">
                    <span class="speet-username"><%= s.getUsername() %></span><br />
                    <span class="speet-message"><%= s.getMessage() %></span><br />
                    <span class="speet-timestamp"><%= s.getTimeStamp() %></span>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <hr />
                </td>
            </tr>
        </table>
                
        <% } } %>   
        </div> 
    </div>
    </body>
</html>

